<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

function get_category( $model )
{
	$tree = cache_read( 'category_'.$model );
	if( $tree == '' )
	{
		cache_category_tree( $model );
		$tree = cache_read( 'category_'.$model );
	}
	return $tree;
}

function get_document_list( $catid = 0, $start = 0, $set = array('stime'=>'','etime'=>'','keywords'=>'','status'=>'all','model'=>'article') )
{
	global $db, $user_id;
	$and = ' AND ';
	$keywords = '';
	if( $set['keywords'] !== '' )
	{
		$ids_sql = 'SELECT `document_id` FROM `'.DB_PRE.'tag` WHERE `name` = "'.$set['keywords'].'"';
		$ids_array = $this->db->query( $ids_sql );
		$ids_no = count( $ids_array );
		if( $ids_no > 0 )
		{
			$ids = '';
			foreach( $ids_array as $k => $v )
			{
				$ids .= ( $k+1 == $ids_no )?$v['document_id']:$v['document_id'].',';
			}
			$keywords = " AND `id` IN ( $ids ) ";
			unset( $ids_array, $ids, $ids_no );
		}
		else 
		{
			return array( 'documents' => array(), 'documents_no' => 0 );
		}
	}
	$cat = $catid == 0?"`model` = '{$set['model']}'":"`category_id` = $catid ";
	$stime = $set['stime'] == ''?'':' AND `start_time` >= "'.strtotime( $set['stime'] ).'" ';
	$etime = $set['etime'] == ''?'':' AND `end_time` <= "'.strtotime( $set['etime'] ).'" ';
	$status = $set['status'] == 'my'?" AND `user_id` = $user_id ":( $set['status'] == 'all'?'':' AND `status` = "'.$set['status'].'" ' );
	$sql = "SELECT `id`, `title`, `title_color`, `username`, `recommend`, `status`, `post_time` 
			FROM `".DB_PRE."document` WHERE ".$cat.$stime.$etime.$keywords.$status."ORDER BY `id` DESC LIMIT ".$start.", 15";
	$sql_count = "SELECT COUNT(`id`) AS `count` FROM `".DB_PRE."document` WHERE ".$cat.$stime.$etime.$keywords.$status;
	$documents = $db->query( $sql );
	$documents_no = $db->query( $sql_count, true );
	foreach( $documents as $k => $v )
	{
		$documents[$k]['post_time'] = date( 'Y-m-d H:i', $v['post_time'] );
	}
	return array( 'documents' => $documents, 'documents_no' => $documents_no['count'] );
}

function get_category_detial( $id )
{
	global $db;
	return $db->get_one( DB_PRE.'category', array( 'id' => $id ) );
}

function get_one_category_childs( $catid )
{
	global $db;
	$sql = 'SELECT `child_ids` FROM `'.DB_PRE.'category` WHERE `id` = '.$catid;
	$result = $db->query( $sql, true );
	return $result['child_ids'];
}

function get_category_childs( $catid, $model, $get_level = 0 )
{
	global $childs;
	$childs = array();
	_get_category_childs( $catid, $model, $get_level );
	return $childs;
}

function _get_category_childs( $pid, $model, $get_level, $level = 1 )
{
	global $childs, $db;
	$sql = "SELECT `id`, `pid`, `path` FROM `".DB_PRE."category` WHERE `pid` = '$pid' AND `model` = '$model' ORDER BY `order` ASC";
	$category = $db->query( $sql );
	foreach( $category as $key=>$value )
	{
		$childs[] = $value;
		( $get_level == 0 || $get_level > $level ) && _get_category_childs( $value['id'], $model, $get_level, $level+1 );
	}
}

function insert_category( $data )
{
	global $db;
	if( $data['pid'] != 0 )
	{
		$sql = "SELECT `child_ids`, `path` FROM `".DB_PRE."category` WHERE `id` = ".$data['pid'];
		$parent_info = $db->query( $sql, true );
		$data['path'] = $parent_info['path'].'/'.$data['dir'];
	}
	else
	{
		$data['path'] = $data['dir'];
	}
	create_dir( SITE_PATH.$data['path'] );
	$result = $db->insert( DB_PRE.'category', $data );
	if( $result  )
	{
		$catid = $db->insert_id();
		if( $data['pid'] != 0 )
		{
			$child_ids = $parent_info['child_ids'] == ''?$catid:$parent_info['child_ids'].','.$catid;
			$db->update( DB_PRE.'category', array( 'child' => 1, 'child_ids' => $child_ids ), array( 'id' => $data['pid'] ) );
		}
		return $catid;
	}
	else
	{
		return false;
	}
}

function delete_category( $catid, $pid, $path )
{
	global $db;
	$result = $db->delete( DB_PRE.'category', array( 'id' => $catid ) );
	if( $result && $pid != 0 )
	{
		return ( $result && update_parent_category_child_ids( $pid, $catid ) )?true:false;
	}
	delete_files( SITE_PATH.$path, true );
	return $result; 
}

/********* ^^^^^ 未测试  ^^^^^ *********/
function move_category( $catid, $pid, $move_to_id, $path )
{
	global $db;
	$sql = "SELECT `child_ids`, `path` FROM `".DB_PRE."category` WHERE `id` = $move_to_id";
	$m_result = $db->query( $sql, true );
	
	$path_end = explode( '/', $path );
	$path_end = array_pop( $path_end );
	$new_path = $m_result['path'].'/'.$path_end;
	
	rename_dir( $path, $new_path );

	$result = $db->update( DB_PRE.'category', array( 'pid' => $move_to_id, 'path' => $new_path ), array( 'id' => $catid ) );

	$child_ids = $m_result['child_ids'].','.$catid;
	$m_update = $db->update( DB_PRE.'category', array( 'child' => 1, 'child_ids' => $child_ids ), array( 'id' => $pid ) );

	if( $result && $m_update && $pid != 0 )
	{
		return ( $result && $m_update && update_parent_category_child_ids( $pid, $catid ) )?true:false;
	}

	return ( $result && $m_update )?true:false;
}

function update_parent_category_child_ids( $pid, $catid )
{
	global $db;
	$sql = "SELECT `child_ids` FROM `".DB_PRE."category` WHERE `id` = $pid";
	$p_result = $db->query( $sql, true );
	$child_ids = explode( ',', $p_result['child_ids'] );
	$child = count( $child_ids ) > 1?1:0;
	foreach( $child_ids as $k => $v )
	{
		if( $v == $catid )
		{
			unset( $child_ids[$k] );
		}
	}
	$child_ids = implode( ',', $child_ids );
	return $db->update( DB_PRE.'category', array( 'child' => $child, 'child_ids' => $child_ids ), array( 'id' => $pid ) );
}

/********* ^^^^^ 未测试  ^^^^^ *********/
function move_category_document( $catid, $move_to_id )
{
	global $db;
	return $db->update( DB_PRE.'document', array( 'category_id' => $move_to_id ), array( 'category_id' => $catid ) );
}

/********* ^^^^^ 未完成/未测试  ^^^^^ *********/
function empty_category_document( $catid )
{
	global $db;
	$sql = "SELECT `id` FROM `".DB_PRE."document` WHERE `category_id` = $catid";
	$documents = $db->query( $sql );
	return true;
}


















/******************************************************** dialog ***********************************************************/

function get_special_list()
{
	global $db;
	$sql = "SELECT `id`, `name` FROM `".DB_PRE."special`";
	return $db->query( $sql );
}

function get_article( $id, $page )
{
	global $db;
	$sql = "SELECT `a`.`id`, `a`.`post_time`, `a`.`author`, `a`.`page_no`, `a`.`tags`, `a`.`from`, `a`.`from_url`, `a`.`title_color`, `a`.`title`, `b`.`username` AS `editor`, `c`.`content` FROM `".DB_PRE."article` AS `a` 
			LEFT JOIN `".DB_PRE."member` AS `b` ON `a`.`uid` = `b`.`id` 
			LEFT JOIN `".DB_PRE."articlecontent` AS `c` ON `c`.`page` = $page AND `c`.`aid` = $id 
			WHERE `a`.`id` = $id";
	$result = $db->query( $sql, true );
	$result['post_time'] = date( 'Y-m-d H:i:s', $result['post_time'] );
	return $result;
}